<?php
#                                                                                 
# author: Cody Sorgenfrey                                       
# (c)2012 South Hill Calvary Chapel                                               
#                                                                                 

include_once("./root.inc.php");
include_once("${root}/mysql/database_info.php");
include_once("${root}/mysql/common.php");
include_once("${root}/common.php");

if ($_REQUEST['librarian_cookie'] != 1) header("location: http://www.southhillcalvary.org/404error.php");

function tableContainsOverDueBook($table) {
	
	$test = mysql_query("SELECT * FROM `SHCCLibrary_ ".$table."` WHERE `overDue`='1'");
	
	if (mysql_num_rows($test) != 0){
		
		return TRUE;
	}
	else {
		
		return FALSE;
	}
}
function checkOutBookForm (){		
	
	printf("\n</br><h4>Check out book:</h4></br>\n\n");
	printf("\t<form action='librarydetail.php' method='get'>\n");
	printf("\t\tFull Name: <input type='text' name='fullName' /> Email: <input type='text' name='email' /> <input type='hidden' name='checkOutBookForm' value='1' /><input type='hidden' name='title' value='%s' /> <input type='hidden' name='id' value='%s' /> \t<input type='submit' value='Check out' />\n</form></br>\n\n",urlencode(stripcslashes($_REQUEST['title'])) ,$_REQUEST['id']);
}
function editGenreForm() {
	
	$currGenre = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE `id`='".$_REQUEST['id']."'"));
	
	if ($currGenre['genre'] == ""){
		
		printf("<h5>Genre not set...</h5><br>\n");
	}
	else {
		
		printf("<h5>Genre is currently set to \"%s\"...</h5>\n<br>", $currGenre['genre']);
	}
	
	printf("<form action='librarydetail.php'>\n");
		
		if ($currGenre['genre'] == "") {
			
			printf("\tSet to:<select name='newGenre'>\n");
		}
		else{
			
			printf("\tUpdate to:<select name='newGenre'>\n");
		}		
				
				printf("\t\t <option value='#'></option>\n");
				printf("\t\t <option value='Apologetics'>Apologetics</option>\n");
				printf("\t\t <option value='Bible Study'>Bible Study</option>\n");
				printf("\t\t <option value='Biography'>Biography</option>\n");
				printf("\t\t <option value='ChristianLiving'>Christian Living</option>\n");
				printf("\t\t <option value='Classics'>Classics</option>\n");
				printf("\t\t <option value='Commentary'>Commentary</option>\n");
				printf("\t\t <option value='Counseling'>Counseling</option>\n");
				printf("\t\t <option value='CreationScience'>Creation & Science</option>\n");
				printf("\t\t <option value='CultsWorldReligions'>Cults & World Religions</option>\n");
				printf("\t\t <option value='CurrentEvents'>Current Events & End Times</option>\n");
				printf("\t\t <option value='Dating'>Dating</option>\n");
				printf("\t\t <option value='Devotionals'>Devotionals</option>\n");
				printf("\t\t <option value='Marriage'>Marriage</option>\n");
				printf("\t\t <option value='Evangelism'>Evangelism</option>\n");
				printf("\t\t <option value='Fiction'>Fiction</option>\n");
				printf("\t\t <option value='Financial '>Financial</option>\n");
				printf("\t\t <option value='General'>General</option>\n");
				printf("\t\t <option value='HolySpirit'>Holy Spirit</option>\n");
				printf("\t\t <option value='Leadership'>Leadership</option>\n");
				printf("\t\t <option value='MarriageFamilyParenting'>Marriage & Family/Parenting</option>\n");
				printf("\t\t <option value='Men'>Mens</option>\n");
				printf("\t\t <option value='Missions'>Missions</option>\n");
				printf("\t\t <option value='Prayer'>Prayer</option>\n");
				printf("\t\t <option value='Questions'>Questions</option>\n");
				printf("\t\t <option value='Reference'>Reference</option>\n");
				printf("\t\t <option value='AV'>Video & Audio</option>\n");
				printf("\t\t <option value='Women'>Womens</option>\n");
		printf("\t</select>\n");
		
		printf("\t<input type='hidden' name='title' value='%s' />", stripcslashes($_REQUEST['title']));
		
		printf("\t<input type='hidden' name='id' value='%s' />", $_REQUEST['id']);
				
		if ($currGenre['genre'] == "") {
			
			printf("\t<input type='submit' value='Set' />\n");
		}
		else{
			
			printf("\t<input type='submit' value='Update' />\n");
		}	
	printf("</form><br>\n\n");
}
function checkOutBook() {
	
		$checkOutBookById = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE `id`='" . $_REQUEST['id'] . "'"));
	
		$checkOutBookById['copies_avail'] -= 1;
	
		mysql_query("UPDATE `SHCCLibrary` SET `copies_avail` = '". $checkOutBookById['copies_avail'] ."' WHERE `id` = '". $_REQUEST['id'] ."'");
	
		mysql_query("UPDATE `SHCCLibrary` SET `who_has`='" .$_REQUEST['fullName']. "' WHERE `id`='" . $_REQUEST['id'] . "'");
	
		$checkOutDate = date("Y-m-d", strtotime("today"));
	
		$returnDate = date("Y-m-d", strtotime("+1 month"));
	
		mysql_query("UPDATE `SHCCLibrary` SET `checkout` ='" . $checkOutDate . "' WHERE `id`='" . $_REQUEST['id'] . "'");
	
		mysql_query("UPDATE `SHCCLibrary` SET `due` ='" . $returnDate . "' WHERE `id`='" . $_REQUEST['id'] . "'");
		
		mysql_query("UPDATE `SHCCLibrary` SET `email` ='" .$_REQUEST['email']. "' WHERE `id`='" . $_REQUEST['id'] . "'");

		printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Checkout+Successful!+Due+on+%s\"); </script>", date("F d, Y", strtotime($returnDate)));

}
function checkInBook() {
	
	$checkInBookById = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE id='". $_REQUEST['id'] ."'"));
	
	$checkInBookById['copies_avail'] += 1;
	
	mysql_query("UPDATE `SHCCLibrary` SET `copies_avail`='". $checkInBookById['copies_avail'] ."' WHERE id='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary` SET `who_has`='Available' WHERE id='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary` SET `checkout`='' WHERE id='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary` SET `due`='' WHERE id='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary` SET `email`='' WHERE id='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary` SET `overDue`='0' WHERE id='" . $_REQUEST['id'] . "'");
	
	printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Returned!\"); </script>");
}
function deleteBook() {
	mysql_query("DELETE FROM `SHCCLibrary` WHERE `id`='" . $_REQUEST['id'] . "'");
	printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Deleted!\"); </script>");
}
function editGenre() {
	
	mysql_query("UPDATE `SHCCLibrary` SET `genre` = '". $_REQUEST['newGenre'] ."' WHERE `id` = '". $_REQUEST['id'] ."'");
			
	printf("<script type='text/javascript'>refreshPage(\"/librarydetail.php?message=Genre+set!&title=%s&id=%s\")</script>", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id']);
}
function renewBook() {
	
	$checkOutDate = date("Y-m-d", strtotime("today"));
	
	$returnDate = date("Y-m-d", strtotime("+1 month"));
	
	mysql_query("UPDATE `SHCCLibrary` SET `checkout` ='" . $checkOutDate . "' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary` SET `due` ='" . $returnDate . "' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary` SET `overDue` ='0' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Renewed!+Due+on+%s\"); </script>", date("F d, Y", strtotime($returnDate)));
}
function addNewCopyForm() {
	
	printf("<form action='librarydetail.php'>\n");
		printf("\tNumber of copies:<input type='text' name='numCopies' \>\n");
		printf("\t<input type='hidden' name='title' value='%s' \>\n", stripcslashes($_REQUEST['title']));
		printf("\t<input type='hidden' name='id' value='%s' \>\n", $_REQUEST['id']);
		printf("\t<input type='submit' value='Add' \>\n");
	printf("</form><br>\n\n");
}
function addNewCopy($newNumCopies) {

	$book = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE `id`='".$_REQUEST['id']."'"));
	
	$newNumOrigCopies = $book['orig_copies'] + $newNumCopies;
		
	$newNumCopiesAvail = $book['copies_avail'] + $newNumCopies;
			
	mysql_query("CREATE TABLE `SHCCLibrary_ ".$book['id']."` (`id` int(10) unsigned NOT NULL auto_increment, `title` text NOT NULL, `author` text NOT NULL, `orig_copies` int(11) unsigned NOT NULL default '1', `copies_avail` int(4) unsigned NOT NULL default '1', `genre` text NOT NULL, `who_has` text NOT NULL, `checkout` date NOT NULL default '0000-00-00', `due` date NOT NULL default '0000-00-00', `email` text NOT NULL, `overDue` int(4) unsigned NOT NULL default '0', PRIMARY KEY  (`id`))");
					
	for ($g=1; $g<=$newNumOrigCopies; $g++){
		
		mysql_query("INSERT INTO `SHCCLibrary_ ".$book['id']."` (title, author, orig_copies, copies_avail, genre, who_has, checkout, due, email, overDue) VALUES ('" . $book['title'] . " - copy " . $g . "','" . $book['author'] . "','" . $newNumOrigCopies . "','1','', 'Available', '', '', '', '0')");
	}
	
	if ($book['copies_avail'] == 0) {
		
		mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `copies_avail` = '0' WHERE `title` = '". $book['title']." - copy 1'");
		
		mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `who_has` = '".$book['who_has']."' WHERE `title` = '". $book['title']." - copy 1'");
		
		mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `checkout` = '".$book['checkout']."' WHERE `title` = '". $book['title']." - copy 1'");
		
		mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `due` = '".$book['due']."' WHERE `title` = '". $book['title']." - copy 1'");
		
		mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `email` = '".$book['email']."' WHERE `title` = '". $book['title']." - copy 1'");
		
		mysql_query("UPDATE `SHCCLibrary_ ".$book['id']."` SET `overDue` = '".$book['overDue']."' WHERE `title` = '". $book['title']." - copy 1'");
	}
		
	mysql_query("UPDATE `SHCCLibrary` SET `orig_copies` = '". $newNumOrigCopies ."' WHERE `id` = '". $book['id'] ."'");
		
	mysql_query("UPDATE `SHCCLibrary` SET `copies_avail` = '". $newNumCopiesAvail ."' WHERE `id` = '". $book['id'] ."'");
	
	printf("<script type='text/javascript'>refreshPage(\"/librarydetailmultiple.php?message=Book(s)+Added!&title=%s&id=%s\");</script>\n\n", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id']);
}

function checkOutBookFormMulti (){		
	
	printf("\n</br><h4>Check out book:</h4></br></br>\n\n");
	printf("\t<form action='librarydetail.php' method='get'>\n");
	printf("\t\tFull Name: <input type='text' name='fullName' /> Email: <input type='text' name='email' /><input type='hidden' name='checkOutBookForm' value='1' /><input type='hidden' name='title' value='%s' /> <input type='hidden' name='oldId' value='%s' /> <input type='hidden' name='id' value='%s' /> \t<input type='submit' value='Check out' />\n</form></br>\n\n", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['oldId'], $_REQUEST['id']);
}
function editGenreFormMulti() {
	
	$currGenre = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE `id`='".$_REQUEST['oldId']."'"));
	
	if ($currGenre['genre'] == ""){
		
		printf("<h5>Genre not set...</h5><br>\n");
	}
	else {
		
		printf("<h5>Genre is currently set to \"%s\"...</h5>\n<br>", $currGenre['genre']);
	}
	
	printf("<form action='librarydetail.php'>\n");
		
		if ($currGenre['genre'] == "") {
			
			printf("\tSet to:<select name='newGenre'>\n");
		}
		else{
			
			printf("\tUpdate to:<select name='newGenre'>\n");
		}		
				
				printf("\t\t <option value='#'></option>\n");
				printf("\t\t <option value='Apologetics'>Apologetics</option>\n");
				printf("\t\t <option value='Bible Study'>Bible Study</option>\n");
				printf("\t\t <option value='Biography'>Biography</option>\n");
				printf("\t\t <option value='ChristianLiving'>Christian Living</option>\n");
				printf("\t\t <option value='Classics'>Classics</option>\n");
				printf("\t\t <option value='Commentary'>Commentary</option>\n");
				printf("\t\t <option value='Counseling'>Counseling</option>\n");
				printf("\t\t <option value='CreationScience'>Creation & Science</option>\n");
				printf("\t\t <option value='CultsWorldReligions'>Cults & World Religions</option>\n");
				printf("\t\t <option value='CurrentEvents'>Current Events & End Times</option>\n");
				printf("\t\t <option value='Dating'>Dating</option>\n");
				printf("\t\t <option value='Devotionals'>Devotionals</option>\n");
				printf("\t\t <option value='Marriage'>Marriage</option>\n");
				printf("\t\t <option value='Evangelism'>Evangelism</option>\n");
				printf("\t\t <option value='Fiction'>Fiction</option>\n");
				printf("\t\t <option value='Financial '>Financial</option>\n");
				printf("\t\t <option value='General'>General</option>\n");
				printf("\t\t <option value='HolySpirit'>Holy Spirit</option>\n");
				printf("\t\t <option value='Leadership'>Leadership</option>\n");
				printf("\t\t <option value='MarriageFamilyParenting'>Marriage & Family/Parenting</option>\n");
				printf("\t\t <option value='Men'>Mens</option>\n");
				printf("\t\t <option value='Missions'>Missions</option>\n");
				printf("\t\t <option value='Prayer'>Prayer</option>\n");
				printf("\t\t <option value='Questions'>Questions</option>\n");
				printf("\t\t <option value='Reference'>Reference</option>\n");
				printf("\t\t <option value='AV'>Video & Audio</option>\n");
				printf("\t\t <option value='Women'>Womens</option>\n");
		printf("\t</select>\n");
		
		printf("\t<input type='hidden' name='title' value='%s' />", stripcslashes($_REQUEST['title']));
		
		printf("\t<input type='hidden' name='id' value='%s' />", $_REQUEST['id']);
		
		printf("\t<input type='hidden' name='oldId' value='%s' />", $_REQUEST['oldId']);
		
		if ($currGenre['genre'] == "") {
			
			printf("\t<input type='submit' value='Set' />\n");
		}
		else{
			
			printf("\t<input type='submit' value='Update' />\n");
		}	
	printf("</form><br>\n\n");

}
function checkOutBookMulti() {
	
	$checkOutBookById = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` WHERE `id`='".$_REQUEST['id']."'"));
	
	$checkOutBookById['copies_avail'] -= 1;
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `copies_avail` = '". $checkOutBookById['copies_avail'] ."' WHERE `id` = '". $_REQUEST['id'] ."'");
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `who_has`='" . urldecode($_REQUEST['fullName']) . "' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	$checkOutDate = date("Y-m-d", strtotime("today"));
	
	$returnDate = date("Y-m-d", strtotime("+1 month"));
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `checkout` ='" . $checkOutDate . "' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `due` ='" . $returnDate . "' WHERE `id`='" . $_REQUEST['id'] . "'");
		
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `email` ='" .$_REQUEST['email']."' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	$numAvail = mysql_num_rows(mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` WHERE `copies_avail`='1'"));
		
	mysql_query("UPDATE `SHCCLibrary` SET `copies_avail`='". $numAvail ."' WHERE `id`='".$_REQUEST['oldId']."'");

	printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Checkout+Successful!+Due+on+%s\"); </script>", date("F d, Y", strtotime($returnDate)));


}
function checkInBookMulti() {
	
	$checkInBookById = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE id='". $_REQUEST['id'] ."'"));
	
	$checkInBookById['copies_avail'] += 1;
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `copies_avail`='". $checkInBookById['copies_avail'] ."' WHERE id='" . $_REQUEST['id'] . "'");
												 
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `who_has`='Available' WHERE id='" . $_REQUEST['id'] . "'");
												 
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `checkout`='' WHERE id='" . $_REQUEST['id'] . "'");
												 
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `due`='' WHERE id='" . $_REQUEST['id'] . "'");
										
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `email`='' WHERE id='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `overDue`='0' WHERE id='" . $_REQUEST['id'] . "'");
	
	$numAvail = mysql_num_rows(mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` WHERE `copies_avail`='1'"));
		
	mysql_query("UPDATE `SHCCLibrary` SET `copies_avail`='". $numAvail ."' WHERE `id`='".$_REQUEST['oldId']."'");
	
	if (tableContainsOverDueBook($_REQUEST['oldId'])) 	mysql_query("UPDATE `SHCCLibrary` SET `overDue`='1' WHERE `id`='".$_REQUEST['oldId']."'");
	
	else 	mysql_query("UPDATE `SHCCLibrary` SET `overDue`='0' WHERE `id`='".$_REQUEST['oldId']."'");
	
	printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Returned!\"); </script>");
}
function deleteBookMulti() {
	
	$checkTable = mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` ORDER BY `title` ASC ");
	
	if (mysql_num_rows($checkTable) == 2) {
		
		$bookToBeDeleted = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` WHERE `id`='".$_REQUEST['id']."'"));
		
		$otherBook = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` WHERE `id`!='".$_REQUEST['id']."'"));
		
		if ($otherBook['copies_avail'] == 0){
						
			mysql_query("UPDATE `SHCCLibrary` SET `orig_copies`='1' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("UPDATE `SHCCLibrary` SET `copies_avail`='0' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("UPDATE `SHCCLibrary` SET `who_has`='".$otherBook['who_has']."' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("UPDATE `SHCCLibrary` SET `checkout`='".$otherBook['checkout']."' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("UPDATE `SHCCLibrary` SET `due` ='".$otherBook['due']."' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("UPDATE `SHCCLibrary` SET `email`='".$otherBook['email']."' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("UPDATE `SHCCLibrary` SET `overDue`='".$otherBook['overDue']."' WHERE `id`='".$_REQUEST['oldId']."'");
						
			mysql_query("DROP TABLE `SHCCLibrary_ ".$_REQUEST['oldId']."`");
		}
		else {
			
			mysql_query("UPDATE `SHCCLibrary` SET `orig_copies`='1' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("UPDATE `SHCCLibrary` SET `copies_avail`='1' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("UPDATE `SHCCLibrary` SET `overDue`='0' WHERE `id`='".$_REQUEST['oldId']."'");
			
			mysql_query("DROP TABLE `SHCCLibrary_ ".$_REQUEST['oldId']."`");
		}
	}
	
	else {
	
		mysql_query("DELETE FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` WHERE id='" . $_REQUEST['id'] . "'");
		
		$numOrig = mysql_num_rows(mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` ORDER BY `id` ASC"));
				
		mysql_query("UPDATE `SHCCLibrary` SET `orig_copies`='".$numOrig."' WHERE `id`='".$_REQUEST['oldId']."'");
		
		$booksToUnderGoATitleChange = mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` ORDER BY `id` ASC");
					
		$newTitle = substr_replace($_REQUEST['title'], "", -9);
		
		$y = 1;

		while ($result = mysql_fetch_array($booksToUnderGoATitleChange)) {
						
			mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `title`='".$newTitle." - copy ".$y."' WHERE `id`='".$result['id']."'");
			
			$y++;
		}
		
		$numAvailable = mysql_num_rows(mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` WHERE `copies_avail`=1"));
				
		mysql_query("UPDATE `SHCCLibrary` SET `copies_avail`='".$numAvailable."' WHERE `id`='".$_REQUEST['oldId']."'");
		
		if (tableContainsOverDueBook($_REQUEST['oldId'])) 	mysql_query("UPDATE `SHCCLibrary` SET `overDue`='1' WHERE `id`='".$_REQUEST['oldId']."'");
	
		else 	mysql_query("UPDATE `SHCCLibrary` SET `overDue`='0' WHERE `id`='".$_REQUEST['oldId']."'");
		
	}	
			
	printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Deleted!\"); </script>");
}
function editGenreMulti() {
	
	mysql_query("UPDATE `SHCCLibrary` SET `genre` = '". $_REQUEST['newGenre'] ."' WHERE `id` = '". $_REQUEST['oldId'] ."'");
			
	printf("<script type='text/javascript'>refreshPage(\"/librarydetail.php?message=Genre+set!&title=%s&id=%s&oldId=%s\")</script>", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id'], $_REQUEST['oldId']);

}
function renewBookMulti() {
	
	$checkOutDate = date("Y-m-d", strtotime("today"));
	
	$returnDate = date("Y-m-d", strtotime("+1 month"));
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `checkout` ='" . $checkOutDate . "' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `due` ='" . $returnDate . "' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	mysql_query("UPDATE `SHCCLibrary_ ".$_REQUEST['oldId']."` SET `overDue` ='0' WHERE `id`='" . $_REQUEST['id'] . "'");
	
	if (tableContainsOverDueBook($_REQUEST['oldId'])) 	mysql_query("UPDATE `SHCCLibrary` SET `overDue`='1' WHERE `id`='".$_REQUEST['oldId']."'");
	
	else 	mysql_query("UPDATE `SHCCLibrary` SET `overDue`='0' WHERE `id`='".$_REQUEST['oldId']."'");
	
	printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Renewed!+Due+on+%s\"); </script>", date("F d, Y", strtotime($returnDate)));

}

function checkOutBookButton() {
	
	if ($_REQUEST['oldId']){
		
		printf("<button onclick='refreshPage(\"/librarydetail.php?checkOutBookForm=1&title=%s&id=%s&oldId=%s\")'>Check Out Book</button>\n\n",urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id'],$_REQUEST['oldId']);
	}
	else {
		
		printf("<button onclick='refreshPage(\"/librarydetail.php?checkOutBookForm=1&title=%s&id=%s\")'>Check Out Book</button>\n\n", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id']);
	}
}
function editGenreButton() {
	
	if ($_REQUEST['oldId']){
		
		printf("<button onclick='refreshPage(\"/librarydetail.php?editGenre=1&title=%s&id=%s&oldId=%s\")'>Edit Book Genre</button>\n\n",urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id'],$_REQUEST['oldId']);
	}
	else {
		
		printf("<button onclick='refreshPage(\"/librarydetail.php?editGenre=1&title=%s&id=%s\")'>Edit Book Genre</button>\n\n", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id']);
	}

}
function renewBookButton() {
	
	if ($_REQUEST['oldId']){
		
		printf("<button onclick='refreshPage(\"/librarydetail.php?renewBook=1&title=%s&id=%s&oldId=%s\")'>Renew Book</button>\n\n",urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id'],$_REQUEST['oldId']);
	}
	else {
		
		printf("<button onclick='refreshPage(\"/librarydetail.php?renewBook=1&title=%s&id=%s\")'>Renew Book</button>\n\n", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id']);
	}
}
function addNewCopyButton() {
	
	printf("<button onclick='refreshPage(\"/librarydetail.php?addCopies=1&title=%s&id=%s\")'>Add copy/copies</button>\n", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id']);
}
function checkInBookButton (){	

	if ($_REQUEST['oldId']){
		printf("<button onclick='refreshPage(\"/librarydetail.php?title=%s&id=%s&oldId=%s&checkInBook=TRUE\")'>Check In Book</button>\n\n",urlencode(stripcslashes($_REQUEST['title'])),$_REQUEST['id'], $_REQUEST['oldId']);
	}
	else{
		printf("<button onclick='refreshPage(\"/librarydetail.php?title=%s&id=%s&checkInBook=TRUE\")'>Check In Book</button>\n\n", urlencode($_REQUEST['title']), $_REQUEST['id']);
	}
}
function deleteBookButton (){

	if ($_REQUEST['oldId']){

		printf("<button onclick='warnDelete(\"TRUE\",\"%s\",\"%s\",\"%s\")'>Delete Book</button>",$_REQUEST['title'], $_REQUEST['id'],$_REQUEST['oldId']);
	}
	else{
		printf("<button onclick='warnDelete(\"FALSE\",\"%s\",\"%s\",\"NULL\")'>Delete Book</button>",urldecode($_REQUEST['title']), $_REQUEST['id']);
	}
}


$title = "Book Functions for \"" .stripcslashes($_GET['title']). "\"";
$meta_key='books, lend, borrow, christian bookstore';
$meta_desc="Check in or out books.";
print_html_head($title ,$meta_key,$meta_desc);

print_java_head(null);
begin_body();
page_title($title);

connect_select_test("SHCCLibrary");

if ($_REQUEST['message']) printf("<h4 style='color:red;'><b>%s</b></h4><br>", $_REQUEST['message']);

$resultArray = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE `id`='". $_REQUEST['id'] ."'"));

if ($resultArray['orig_copies'] != 1) {

	$resultArray = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['oldId']."` WHERE `id`='". $_REQUEST['id'] ."'"));

	printf("<button style='float:right;' onclick='refreshPage(\"/librarydetailmultiple.php?title=%s&id=%s\")'>Back</button>\n\n", urlencode(substr_replace(stripcslashes($_REQUEST['title']), "", -9)), $_REQUEST['oldId']);

	if ($_REQUEST['editGenre']) editGenreFormMulti();
	
	if ($_REQUEST['newGenre']) editGenreMulti();
	
	if ($resultArray['copies_avail'] == 1) {
			
		if ($_REQUEST['checkOutBookForm']) checkOutBookFormMulti();

		else checkOutBookButton();
									
		if ($_REQUEST['fullName'] && $_REQUEST['email']){
		
			if (strpos($_REQUEST['email'], "@", 1) && strpos($_REQUEST['email'], ".", 3)){
				
				checkOutBookMulti();
			}
			else {
				printf("<div><h4 style='color:red;'>Invalid Email</h4></div><br>\n");
			}
		}
		else if ($_REQUEST['fullName']) printf("<div><h4 style='color:red;'>Invalid Email</h4></div><br>\n");
		
		else if ($_REQUEST['email']) printf("<div><h4 style='color:red;'>Invalid Name</h4></div><br>\n");
	}	
	else {
			
		checkInBookButton();
						
		if ($_REQUEST['checkInBook'] == TRUE) checkInBookMulti();
		
		renewBookButton();
		
		if ($_REQUEST['renewBook'] == 1) renewBookMulti();
	}
	if (!$_REQUEST['editGenre']) editGenreButton();
			
	deleteBookButton();
	
	if ($_REQUEST['deleteBook'] == TRUE){
		
		deleteBookMulti();
	}
} 
else {

	printf("<button style='float:right;' onclick='refreshPage(\"/lendinglibrary.php\")'>Back</button>\n\n");

	if ($_REQUEST['editGenre']) editGenreForm();
		
	if ($_REQUEST['addCopies'] == 1) addNewCopyForm();
	
	if ($resultArray['copies_avail'] == 1) { 
	
		if ($_REQUEST['checkOutBookForm']) checkOutBookForm();
		
		else checkOutBookButton(); 
								
		if ($_REQUEST['fullName'] && $_REQUEST['email']){ 
		
			if (strpos($_REQUEST['email'], "@", 1) && strpos($_REQUEST['email'], ".", 3)) {
		
				checkOutBook();
				}
			else {
			
				printf("<div><h4 style='color:red;'>Invalid Email</h4></div><br>\n");
			}
		}
		else if ($_REQUEST['email']) {
		
			printf("<div><h4 style='color:red;'>Invalid Name</h4></div><br>\n");
		}
		else if ($_REQUEST['fullName']){
		
			printf("<div><h4 style='color:red;'>Invalid Email</h4></div><br>\n");
		}
	}	
	
	else { 
	
		checkInBookButton();
	
		if ($_REQUEST['checkInBook'] == TRUE) checkInBook();
		
		renewBookButton();
		
		if ($_REQUEST['renewBook'] == 1) renewBook();
	}
	if (!$_REQUEST['editGenre']) editGenreButton();
	
	if ($_REQUEST['newGenre']) editGenre(); 
	
	deleteBookButton();
	
	if ($_REQUEST['deleteBook'] == TRUE) deleteBook();
	
	addNewCopyButton();
	
	if ($_REQUEST['numCopies']) addNewCopy($_REQUEST['numCopies']);

}


mysql_close();
end_page_title();
end_body();

?>